package com.glorystone.ins.domain;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Created by wangwei on 2018/3/14.
 */
public @Data class CreditTotal {

    /**
     * db - model
     */

    private Long tid;
    /**
     * 渠道id
     */
    private Long channelId;
    /**
     * 渠道申请授信金额
     */
    private BigDecimal applyAmount;
    /**
     * 申请授信期限，单位月
     */
    private Integer applyPeriod;
    /**
     * 审批通过授信金额
     */
    private BigDecimal acceptAmount;
    /**
     * 审批通过授信期限， 单位月
     */
    private Integer acceptPeriod;
    /**
     * 授信剩余金额
     */
    private BigDecimal lastAmount;
    /**
     * 授信累计使用金额
     */
    private BigDecimal addUpAmount;

    /**
     * 累计已还金额
     */
    private BigDecimal addUpReturnAmount;

    /**
     * 授信起始日期
     */
    @JsonSerialize(using = CustomLocalDateSerializer.class)
    private LocalDate startDate;
    /**
     * 授信结束日期
     */
    @JsonSerialize(using = CustomLocalDateSerializer.class)
    private LocalDate endDate;
    /**
     * 保证金比例, %,  不除100的值
     */
    private BigDecimal depositRate;
    /**
     * 应缴保证金额
     */
    private BigDecimal depositAmount;
    /**
     * 剩余保证金金额
     */
    private BigDecimal depositLastAmount;
    /**
     * 上期保证金金额
     */
    private BigDecimal depositPrePhaseAmount;
    /**
     * 收到保证金金额， 审批时候
     */
    private BigDecimal depositInAmount;
    /**
     * 退出保证金金额， 审批时候
     */
    private BigDecimal depositOutAmount;
    /**
     * 状态 1. 生效中  2.失效 3.审批中
     */
    private Integer status;
    private Long createBy;
    @JsonSerialize(using = CustomLocalDateTimeSerializer.class)
    private LocalDateTime createAt;
    private Long updateBy;
    @JsonSerialize(using = CustomLocalDateTimeSerializer.class)
    private LocalDateTime updateAt;






    /**
     * db - params
     */

    /**
     * 授信申请金额， 单位万
     */
    private BigDecimal applyAmountTenThousand;
    /**
     * 授信通过金额， 单位万
     */
    private BigDecimal acceptAmountTenThousand;
    /**
     * 已使用授信额度
     */
    private BigDecimal usedAmountTenThousand;
    /**
     * 应缴纳保证金， 单位万, 根据申请金额算
     */
    private BigDecimal depositAmountTenThousand;

    /**
     * 授信终审时候希望保证金
     */
    private BigDecimal depositExpectAmount;



    public BigDecimal getUsedAmountTenThousand() {
        if (acceptAmount != null && lastAmount != null) {
            return acceptAmount.subtract(lastAmount).divide(BigDecimal.valueOf(10000), 6, BigDecimal.ROUND_HALF_UP);
        }
        return BigDecimal.ZERO;
    }


    public BigDecimal getAcceptAmountTenThousand() {
        if (acceptAmount != null) {
            return acceptAmount.divide(BigDecimal.valueOf(10000), 6, BigDecimal.ROUND_HALF_UP);
        }
        return null;
    }


    public BigDecimal getDepositExpectAmount() {
        if (applyAmount != null) {
            if (depositRate != null) {
                return applyAmount.multiply(depositRate).divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP);
            }
        }
        return null;

    }


    public BigDecimal getApplyAmountTenThousand() {
        if (applyAmount != null) {
            if (depositRate != null) {
                BigDecimal amount = applyAmount.multiply(depositRate).divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP);
                setDepositAmountTenThousand(amount.divide(BigDecimal.valueOf(10000), 6, BigDecimal.ROUND_HALF_UP));
            }
            BigDecimal amount = applyAmount.divide(BigDecimal.valueOf(10000), 6, BigDecimal.ROUND_HALF_UP);
            return amount;
        }
        return null;
    }





    public enum StatusEnum {
        /**
         * 1. 生效中  2.失效 3.审批中
         */
        ON(1),
        OFF(2),
        WORK_FLOW(3);
        private int value;
        StatusEnum(int value) {
            this.value = value;
        }
        public int getValue() {
            return value;
        }

    }




}
